<template>
  <div class="login-container">
    <div class="bg-img"></div>
    <div class="login-bg">
      <div class="text-box">
        <h1> Crazy work Happy life</h1>
        <span>Crazy work Happy life</span>
      </div>
      <div class="login-box">
        <div class="form-box">
          <h2>Logo in here</h2>
          <el-form :model="loginForm" :rules="rules" ref="loginFormRef">
            <el-form-item prop="username">
              <el-input v-model="loginForm.username" placeholder="请输入账号"></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                type="password"
                placeholder="请输入密码"
                v-model="loginForm.password"
                show-password
                @keyup.enter="handleLogin"
              >
                <template #append>
                  <span @click="handleForget">Forget</span>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="remember" :rules="rules.remember">
              <el-radio-group v-model="loginForm.remember">
                <el-radio :label="true" size="large">Remember me</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                style="width: 100%"
                @click="handleLogin"
                :loading="loading"
              >
                Login
              </el-button>
            </el-form-item>
          </el-form>
          <div class="other-login">
            <a href="">
              <span>Third party quick login</span>
              <!-- SVG图标保持不变 -->
            </a>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

const router = useRouter()

// 登录表单数据
const loginForm = ref({
  username: '',
  password: '',
  remember: false
})

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ],
  remember: [
    {
      validator: (rule, value, callback) => {
        if (!value) {
          callback(new Error('必须勾选Remember me才能登录'))
        } else {
          callback()
        }
      },
      trigger: 'change'
    }
  ]
}

const loginFormRef = ref(null)
const loading = ref(false)

// 登录方法
const handleLogin = () => {
  loginFormRef.value.validate(valid => {
    if (!valid) {
      ElMessage.warning('请完成所有必填项')
      return
    }

    loading.value = true

    // 模拟登录验证
    setTimeout(() => {
      if (loginForm.value.username === 'admin' && loginForm.value.password === 'admin') {
        ElMessage.success('登录成功')

        // 存储记住我状态
        if (loginForm.value.remember) {
          localStorage.setItem('remembered', 'true')
          localStorage.setItem('username', loginForm.value.username)
        } else {
          localStorage.removeItem('remembered')
          localStorage.removeItem('username')
        }

        // 跳转到首页
        router.push('/HomeView/observeScreen/')
      } else {
        ElMessage.error('账号或密码错误')
      }
      loading.value = false
    }, 800)
  })
}

// 忘记密码
const handleForget = () => {
  ElMessage.warning('请联系管理员重置密码')
}

// 初始化记住我状态
onMounted(() => {
  // if (localStorage.getItem('remembered') === 'true') {
  //   loginForm.value.remember = true
  //   loginForm.value.username = localStorage.getItem('username') || ''
  // }
})
</script>

<style scoped>
/* 原有样式保持不变 */
.login-container {
  min-width: 100vw;
  min-height: 100vh;
  background: url('@/assets/imgs/bg.jpg') no-repeat center/cover;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-bg {
  min-width: 900px;
  min-height: 450px;
  border: 6px solid lightblue;
  position: relative;
}

.text-box {
  position: absolute;
  top: 15%;
  right: 50%;
  color: #fff;
}

.login-box {
  position: absolute;
  top: 15%;
  left: 55%;
  background-color: lightblue;
  opacity: .9;
  height: 325px;
  width: 285px;
  border-radius: 5px;
}

.form-box {
  padding: 15px;
}

:deep(.el-input-group__append) {
  cursor: pointer;
}

h2 {
  color: #fff;
  text-align: center;
}

:deep(.el-button) {
  width: 100%;
}

.other-login {
  margin-top: 35px;
}

.other-login span {
  color: gray;
}

.other-login svg {
  width: 16px;
  height: 16px;
  margin-left: 8px;
}

/* 新增的记住我错误提示样式 */
:deep(.el-form-item__error) {
  color: #ff4d4f;
  font-size: 12px;
  line-height: 1;
  padding-top: 4px;
}
</style>
